#include <bits/stdc++.h>
using namespace std;
#define int long long
bool dfs(int a,int b,int c)
{
    bool f=0;
    if(a==0)return 1;
    if(b>0)f=f||dfs((a+2)%9,b-1,c);
    if(c>0)f=f||dfs((a+6)%9,b,c-1);
    return f;
}
void solve()
{
    string s;
    cin>>s;
    int sm=0,c2=0,c3=0;
    //unordered_map<char, int>mp;
    for(int i=0;i<s.size();i++)
    {
        if(s[i]=='2')c2++;
        if(s[i]=='3')c3++;
        sm=(sm+s[i]-'0')%9;
    }
    if(c2>9)
    {
        c2%=9;
        c2+=9;
    }
    if(c3>3)
    {
        c3%=3;
        c3+=3;
    }
    if(sm==0)cout<<"YES"<<endl;
    else
    {
        cout<<(dfs(sm,c2,c3)?"YES":"NO")<<endl;
    }
}
signed main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    int T=1;
    cin>>T;
    while(T--)
    {
       solve();
    }
    return 0;
}